package com.shop2cn.iapi.prodstock.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.prodstock.parameter.ReturnStockForBillBean;
import com.shop2cn.iapi.prodstock.service.ReturnStockForBillCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.Shop2cnStockCallService;
import com.ymttest.utils.AgentUsers;
/**
*退款返还商品库存Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ReturnStockForBill{
	private static ReturnStockForBillBean returnstockforbillBean;
	private static ReturnStockForBillCall returnstockforbillCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("退款返还商品库存Test");
	}

	@Before
	public void caseUp() {
		returnstockforbillBean=new ReturnStockForBillBean();
		returnstockforbillCall=new ReturnStockForBillCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(returnstockforbillCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	// 渠道商
		private static int rootUserId = AgentUsers.rootUserId;
		private static String rootUserName = "NickautoTest";
		
		@Test
		@Category({P1.class})
		@TestCase
		public void Tc_ReturnStockForBill_001() {
			Logger.start(true,"下单扣5个真实，5个虚拟、返还只能返还全部");
			try {

				AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
						rootUserName, true);
				addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
				addAgentProductBean.getCatalogList().get(0).setStock(5);
				String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
				ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
				//下单虚拟十件
				String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
				long order = Shop2cnStockCallService.InitReduceStockForOrder(rootUserId,catalogId,productId,10);
				
				returnstockforbillBean.setOrderId(order);
				returnstockforbillBean.setCatalogId(catalogId);
				returnstockforbillBean.setStock(3);
				
				returnstockforbillCall.setData(returnstockforbillBean);
				returnstockforbillCall.callService();			
				Logger.verifyEquals("ok", returnstockforbillCall.getReturnData(), "验证code");
				Thread.sleep(5000);
				ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
				
				Logger.verifyEquals(35,productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
				Logger.verifyEquals(5,productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
				Logger.verifyEquals(30,productDetailsDto2.getCatalogList().get(0).getVirtualStock(),"验证虚拟库存");
			} catch (Exception e) {
				Logger.fail(e);
			}
		}
		
		
		@Test
		@Category({P1.class})
		@TestCase
		public void Tc_ReturnStockForBill_002() {
			Logger.start(true,"下单扣10个真实，返还3个库存");
			try {

				AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
						rootUserName, true);
				addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
				addAgentProductBean.getCatalogList().get(0).setStock(50);
				String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
				ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
				//下单虚拟十件
				String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
				long order = Shop2cnStockCallService.InitReduceStockForOrder(rootUserId,catalogId,productId,10);
				
				returnstockforbillBean.setOrderId(order);
				returnstockforbillBean.setCatalogId(catalogId);
				returnstockforbillBean.setStock(3);
				
				returnstockforbillCall.setData(returnstockforbillBean);
				returnstockforbillCall.callService();			
				Logger.verifyEquals("ok", returnstockforbillCall.getReturnData(), "验证code");
				Thread.sleep(5000);
				ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
				
				Logger.verifyEquals(73,productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
				Logger.verifyEquals(43,productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
				Logger.verifyEquals(30,productDetailsDto2.getCatalogList().get(0).getVirtualStock(),"验证虚拟库存");
			} catch (Exception e) {
				Logger.fail(e);
			}
		}
		
		@Test
		@Category({P1.class})
		@TestCase
		public void Tc_ReturnStockForBill_003() {
			Logger.start(true,"下单扣10个真实，20虚拟，核销后，返还3个，会返还到真实库存");
			try {

				AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
						rootUserName, true);
				addAgentProductBean.getCatalogList().get(0).setVirtualStock(20);
				addAgentProductBean.getCatalogList().get(0).setStock(10);
				String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
				ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
				//下单虚拟十件
				String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
				long order = Shop2cnStockCallService.InitReduceStockForOrder(rootUserId,catalogId,productId,30);
				
				Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 20,order);
				
				returnstockforbillBean.setOrderId(order);
				returnstockforbillBean.setCatalogId(catalogId);
				returnstockforbillBean.setStock(3);
				returnstockforbillCall.setData(returnstockforbillBean);
				returnstockforbillCall.callService();
				Logger.verifyEquals("ok", returnstockforbillCall.getReturnData(), "验证code");
			} catch (Exception e) {
				Logger.fail(e);
			}
		}
		
		
		@Test
		@Category({P1.class})
		@TestCase
		public void Tc_ReturnStockForBill_004() {
			Logger.start(true,"下单扣10个真实，20虚拟，核销后，返还全部，会到真实库存");
			try {
				AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
						rootUserName, true);
				addAgentProductBean.getCatalogList().get(0).setVirtualStock(20);
				addAgentProductBean.getCatalogList().get(0).setStock(10);
				String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
				ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
				//下单虚拟十件
				String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
				long order = Shop2cnStockCallService.InitReduceStockForOrder(rootUserId,catalogId,productId,30);
				Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 30);
				returnstockforbillBean.setOrderId(order);
				returnstockforbillBean.setCatalogId(catalogId);
				returnstockforbillBean.setStock(30);
				returnstockforbillCall.setData(returnstockforbillBean);
				returnstockforbillCall.callService();			
				Logger.verifyEquals("ok", returnstockforbillCall.getReturnData(), "验证code");
			} catch (Exception e) {
				Logger.fail(e);
			}
		}
		
		
		@Test
		@Category({P1.class})
		@TestCase
		public void Tc_ReturnStockForBill_005() {
			Logger.start(true,"下单扣10个真实，20虚拟，核销后，返还全部，会到真实库存");
			try {

				AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
						rootUserName, true);
				addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
				addAgentProductBean.getCatalogList().get(0).setStock(50);
				String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
				ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
				//下单虚拟十件
				String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
				long order = Shop2cnStockCallService.InitReduceStockForOrder(rootUserId,catalogId,productId,30);
				
				Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 30);
				
				returnstockforbillBean.setOrderId(order);
				returnstockforbillBean.setCatalogId(catalogId);
				returnstockforbillBean.setStock(30);
				
				returnstockforbillCall.setData(returnstockforbillBean);
				returnstockforbillCall.callService();			
				Logger.verifyEquals("ok", returnstockforbillCall.getReturnData(), "验证code");
				Thread.sleep(5000);
				ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
				
				Logger.verifyEquals(80,productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
				Logger.verifyEquals(50,productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
				Logger.verifyEquals(30,productDetailsDto2.getCatalogList().get(0).getVirtualStock(),"验证虚拟库存");
			} catch (Exception e) {
				Logger.fail(e);
			}
		}
		
}